package src;

/**
 * 链表
 */
public class LinkedList<E> {

    /**
     * 节点
     */
    private class Node {
        public E e;
        public Node next;

        public Node(E e, Node next){
            this.e = e;
            this.next = next;
        }
        public Node(E e){
            this(e, null);
        }
        public Node(){
            this(null);
        }

        @Override
        public String toString() {
            return e.toString();
        }
    }

    private Node dummyHead; // 虚拟头结点
    private Node tail; // 尾节点
    private int size;

    public LinkedList(){
        dummyHead = new Node();
        tail = dummyHead.next;
        size = 0;
    }

    public void add(int index, E e){
        if(index < 0 || index > size)
            throw new IllegalArgumentException("add failed, index out of range");
        if (tail == null) {
            tail = new Node(e);
            dummyHead.next = tail;
            size ++;
            return ;
        }
        // 在尾部追加
        if (index == size) {
            addLast(e);
            return ;
        }
        // 移动指针位置
        Node prev = dummyHead;
        for (int i = 0; i < index; i++)
            prev = prev.next;
        prev.next = new Node(e, prev.next);
        size ++;
    }
    public void addFirst(E e){
        add(0, e);
    }
    public void addLast(E e){
        if (tail == null) {
            tail = new Node(e);
            dummyHead.next = tail;
        }else{
            tail.next = new Node(e);
            tail = tail.next;
        }
        size ++;
    }

    public E get(int index){
        if (index < 0 || index >= size)
            throw new IllegalArgumentException("get failed, index out of range");
        if (index == size - 1) {
            return getLast();
        }
        Node cur = dummyHead.next;
        for (int i = 0; i < index; i++)
            cur = cur.next;
        return cur.e;
    }

    public E getFirst(){
        return get(0);
    }

    public E getLast(){
        if (tail != null) {
            return tail.e;
        }else{
            return null;
        }
    }

    public void set(int index, E e){
        if(index < 0 || index >= size)
            throw new IllegalArgumentException("set failed, index out of range");
        if (index == size - 1) {
            tail.e = e;
            return ;
        }
        Node cur = dummyHead.next;
        for (int i = 0; i < index; i++)
            cur = cur.next;
        cur.e = e;
    }

    public int find(E e) {
        Node cur = dummyHead.next;
        int index = 0;
        while(cur != null){
            if (cur.e.equals(e)) {
                return index;
            }
            cur = cur.next;
            index ++;
        }
        return -1;
    }

    public E remove(int index) {
        if(index < 0 || index >= size)
            throw new IllegalArgumentException("remove failed, index out of range");
        
        Node prev = dummyHead;
        for (int i = 0; i < index; i++) {
            prev = prev.next;
        }
        Node delNode = prev.next;
        prev.next = delNode.next;
        delNode.next = null;
        if (index == size - 1)
            tail = prev;
        if (dummyHead.next == null)
            tail = null;
        size --;
        return delNode.e;
    }

    public E removeFirst() {
        return remove(0);
    }

    public E removeLast() {
        return remove(size - 1);
    }

    public boolean contains(E e) {
        return -1 != find(e);
    }
    
    public int getSize() {
        return size;
    }

    public boolean isEmpty(){
        return size == 0;
    }

    @Override
    public String toString() {
        StringBuffer res = new StringBuffer();
        Node cur = dummyHead.next;
        int count = 1000;
        while(cur != null){
            res.append(cur + "->");
            cur = cur.next;
            if (count <= 0)
                break;
            count --;
        }
        res.append("NULL");
        return res.toString();
    }
}